package com.kdd.campus.sh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kdd.campus.sh.dao.ShCategoryMapper;
import com.kdd.campus.sh.po.ShCategory;
import com.kdd.campus.sh.service.ShCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:ShCategory业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class ShCategoryServiceImpl implements ShCategoryService {

    @Autowired
    private ShCategoryMapper shCategoryMapper;



    /**
     * ShCategory条件+分页查询
     * @param shCategory 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<ShCategory> findPage(ShCategory shCategory, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(shCategory);
        //执行搜索
        return new PageInfo<ShCategory>(shCategoryMapper.selectByExample(example));
    }

    /**
     * ShCategory分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<ShCategory> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<ShCategory>(shCategoryMapper.selectAll());
    }

    /**
     * ShCategory条件查询
     * @param shCategory
     * @return
     */
    @Override
    public List<ShCategory> findList(ShCategory shCategory){
        //构建查询条件
        Example example = createExample(shCategory);
        //根据构建的条件查询数据
        return shCategoryMapper.selectByExample(example);
    }


    /**
     * ShCategory构建查询对象
     * @param shCategory
     * @return
     */
    public Example createExample(ShCategory shCategory){
        Example example=new Example(ShCategory.class);
        Example.Criteria criteria = example.createCriteria();
        if(shCategory!=null){
            // 
            if(!StringUtils.isEmpty(shCategory.getId())){
                    criteria.andEqualTo("id",shCategory.getId());
            }
            // 父分类id（自连接）
            if(!StringUtils.isEmpty(shCategory.getParentId())){
                    criteria.andEqualTo("parentId",shCategory.getParentId());
            }
            // 商品名称
            if(!StringUtils.isEmpty(shCategory.getName())){
                    criteria.andLike("name","%"+shCategory.getName()+"%");
            }
            // 显示状态(0:不显示 1:显示)
            if(!StringUtils.isEmpty(shCategory.getShowStatus())){
                    criteria.andEqualTo("showStatus",shCategory.getShowStatus());
            }
            // 图标
            if(!StringUtils.isEmpty(shCategory.getIconUrl())){
                    criteria.andEqualTo("iconUrl",shCategory.getIconUrl());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        shCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改ShCategory
     * @param shCategory
     */
    @Override
    public void update(ShCategory shCategory){
        shCategoryMapper.updateByPrimaryKey(shCategory);
    }

    /**
     * 增加ShCategory
     * @param shCategory
     */
    @Override
    public void add(ShCategory shCategory){
        shCategoryMapper.insert(shCategory);
    }

    /**
     * 根据ID查询ShCategory
     * @param id
     * @return
     */
    @Override
    public ShCategory findById(Integer id){
        return  shCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询ShCategory全部数据
     * @return
     */
    @Override
    public List<ShCategory> findAll() {
        return shCategoryMapper.selectAll();
    }

    @Override
    public List<ShCategory> findByLocation(Integer location) {
        ShCategory shCategory = new ShCategory();
        shCategory.setLocation(location);
        shCategory.setParentId(0);
        return shCategoryMapper.select(shCategory);
    }

    @Override
    public int getProductCountByCategory(Integer categoryId) {
        return shCategoryMapper.getProductCountByCategory(categoryId);

    }
}
